<template>
  <div class="home-container">
    <el-container>
      <el-header>
        <div class="header-left">
          <img src="../../assets/logo.png" alt="">
          <span>前端16期后台管理系统</span>
        </div>
        <el-button @click="logout">退出</el-button>
        </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 收缩的面包屑按钮 -->
          <div @click="isCollapse =! isCollapse" class="toggleMenu">|||</div>
           <el-menu
           background-color="#373d41"
           text-color="#fff"
           active-text-color="#409eff"
           class="el-menu-vertical-demo"
           :collapse="isCollapse"
           :collapse-transition="false"
           router
           :default-active="$route.path"
           :unique-opened="true"
          >
          <!-- 循环一级标题 -->
            <el-submenu v-for="item in menuList" :key="item.id" :index="item.path+''" >
            <template slot="title">
              <i :class="iconsList[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 循环二级分类 -->
            <!-- item.children遍历一级菜单每一项下面的children -->
            <!-- 动态绑定index拼接路与地zhi -->
            <el-menu-item v-for="item2 in item.children" :key="item2.id" :index="'/' + item2.path">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{item2.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // data是存储数据
      menuList: [],
      iconsList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false
    }
  },
  // 让页面一加载就获取菜单数据，所以挂载到了生命周期created上
  created () {
    this.getMenuList()
  },
  methods: {
    logout () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 获取菜单数据
    async getMenuList () {
      const { data: res } = await this.$axios.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.menuList = res.data
    }
  }
}
</script>
<style scoped lang='less'>
.home-container {
  height: 100%;
   .el-container{
     height: 100%;
   }
   .el-header {
     background: #373d41;
     display: flex;
     justify-content: space-between;
     align-items: center;
    //  让左侧图片文字跟右侧按钮对齐
     .header-left {
       display: flex;
       align-items: center;
       span {
         font-size: 20px;
         color: #fff;
         margin-left: 10px;
      }
     }
   }
   .el-aside{
     background: #373d41;
     .iconfont{
       margin-right: 10px;
       }
       .el-menu {
         border-right: none;
       }
       .toggleMenu {
         width: 100%;
         height: 40px;
         line-height: 40px;
         text-align: center;
         background: palegoldenrod;
         color: #fff;
         letter-spacing : 5px;
         cursor: pointer;
         user-select: none
       }
   }
   .el-main{
     background:  #eee;
   }
}
</style>
